package com.example.hand.controller;

import com.example.hand.VO.ResultVO;
import com.example.hand.dataobject.CarDetail;
import com.example.hand.dataobject.CarInfo;
import com.example.hand.form.CarInfoForm;
import com.example.hand.form.CarListForm;
import com.example.hand.service.CarInfoService;
import com.example.hand.utils.ResultVOUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

@RestController
@RequestMapping("/carInfo")
public class CarInfoController {

    @Autowired
    private CarInfoService carInfoService;

    @ApiOperation(value="添加车辆信息", notes="添加车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "车辆信息列表", required = true, dataType = "CarInfoForm"),
    })
    @PostMapping("/create")
    public ResultVO create(@Valid CarInfoForm form, BindingResult bindingResult){
        ResultVOUtils resultVOUtils = new ResultVOUtils();
        if(bindingResult.hasErrors()){
            return ResultVOUtils.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        CarDetail carDetail = new CarDetail();
        BeanUtils.copyProperties(form,carDetail);
        CarDetail newCarDetail = carInfoService.add(carDetail);
        return ResultVOUtils.success(newCarDetail);
    }

    @ApiOperation(value="删除车辆", notes="传递car_id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车辆id", required = true, dataType = "Integer"),
    })
    @PostMapping("/delete")
    public ResultVO delete(@RequestParam("Id") Integer Id){
        ResultVOUtils resultVOUtils = new ResultVOUtils();
        if(Id == 0 ){
            return resultVOUtils.error(1,"id不能为0");
        }
        carInfoService.delete(Id);
        return resultVOUtils.success();
    }


    @ApiOperation(value="更新车辆信息", notes="更新车辆信息，全量覆盖")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "车辆信息列表", required = true, dataType = "CarInfoForm"),
    })
    @PostMapping("/update")
    public ResultVO update(@Valid CarInfoForm form, BindingResult bindingResult){
        ResultVOUtils resultVOUtils = new ResultVOUtils();
        if(bindingResult.hasErrors()){
            return ResultVOUtils.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        CarDetail carDetail = new CarDetail();
        BeanUtils.copyProperties(form,carDetail);
        CarDetail carDetail1 = carInfoService.update(carDetail);
        return ResultVOUtils.success(carDetail);
    }

    @ApiOperation(value="车辆列表", notes="条件查询车辆列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "size", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "carListForm", value = "车辆列表实体类", required = false, dataType = "CarListForm")
    })
    @GetMapping("/list")
    public ResultVO getList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                            @RequestParam(value = "size", defaultValue = "10") Integer size,
                            @Valid CarListForm carListForm){
        ResultVOUtils resultVOUtils = new ResultVOUtils();

        //排序
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();

        if(carListForm.getOrderBy() != null){
            if(carListForm.getOrderValue() == null){
                orderList.add(new Sort.Order(Sort.Direction.DESC,carListForm.getOrderBy()));
            }else{
                orderList.add(new Sort.Order(Sort.Direction.ASC,carListForm.getOrderBy()));
            }
        }

        PageRequest request = new PageRequest(page,size,new Sort(orderList));
        //Map<String, Object> params = newHashMap();

        Page<CarInfo> carInfoPage = carInfoService.findListCarInfo(carListForm,request);
        return ResultVOUtils.success(carInfoPage.getContent());
    }


}
